var api,
  util = require("./utils/utils.js")

// 播放器
const innerAudioContext = wx.createInnerAudioContext()
/**
 * 录音播放
 * @param src 音频地址
 * @param duration 音频秒数（单位 S）
 */
let SoundRecordingPlay = function ({ src, duration }) {
  let _src = src;
  let _duration = duration;
  if(_src && _duration){
    innerAudioContext.src = _src
    innerAudioContext.play();
    // setTimeout(function () {
    //   innerAudioContext.stop();
    // }, _duration * 1000)
  }
}

/**
 * 监听通知
 */
function ListeningNotify() {
  new SendRequestGetNotifyStatus();
  setTimeout(ListeningNotify, 20000);
}

/**
 * 执行查询并播放音频
 */
function SendRequestGetNotifyStatus(){
  if (wx.getStorageSync('cash').cashstatus == 1 && wx.getStorageSync('site') != '' && wx.getStorageSync('uid')!=''){
    
    wx.getLocation({
      success: function (res) {
        wx.request({
          url: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/api.php/order/playMsg',
          data: {
            uid: wx.getStorageSync('uid'),
            bid: wx.getStorageSync('bid'),
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (e) {
            if (e.data.code == 1) {
              new SoundRecordingPlay({ src: wx.getStorageSync('site') + "/addons/sd_liferuning/tp/public/static/remind.mp3", duration: "14" });
            }
          }
        })
      }
    })
  }
 
}

/**
 * 监听器运行
 */
new ListeningNotify();

App({
  is_on_launch: !0,
  //  data: {
  //   canIUse: wx.canIUse('button.open-type.getUserInfo')
  // },
  onLaunch: function () {
    this.setApi(),
      api = this.api,
      this.getStoreData();
    this.getUserDataToken();

    
  }
  ,
  getmenu: function () {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ff0000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  },
  getStoreData: function () {
    var t = this;
    if (!wx.getStorageSync('bid')) {
      this.request({
        url: api.default.store,
        success: function (t) {
          wx.setStorageSync('bid', t.bid.bid)
        },
      });
    }

    /****站点*/
    if (!wx.getStorageSync('site')) {
      this.request({
        url: api.default.site,
        success: function (t) {
          // console.log(t.site)
          wx.setStorageSync('site', t.site)
        },
      });
    }
  },
  getUserDataToken: function () {
    var that = this;
    var utoken = wx.getStorageSync("utoken");
  },
  getMenu: function () {

  }
  ,

  request: function (a) {
    a.data || (a.data = {});
    var t = wx.getStorageSync("access_token");
    t && (a.data.access_token = t), a.data._uniacid = this.siteInfo.uniacid, a.data._acid = this.siteInfo.acid,
      wx.request({
        url: a.url,
        header: a.header || {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: a.data || {},
        method: a.method || "GET",
        dataType: a.dataType || "json",
        success: function (t) {
          -1 == t.data.code ? getApp().login() : a.success && a.success(t.data);
        },
        fail: function (t) {
          console.warn("--- request fail >>>"), console.warn(t), console.warn("<<< request fail ---");
          var e = getApp();
          e.is_on_launch ? (e.is_on_launch = !1, wx.showModal({
            title: "网络请求出错",
            content: t.errMsg,
            showCancel: !1,
            success: function (t) {
              t.confirm && a.fail && a.fail(t);
            }
          })) : (wx.showToast({
            title: t.errMsg,
            image: "/images/icon-warning.png"
          }), a.fail && a.fail(t));
        },
        complete: function (t) {
          200 != t.statusCode && (console.log("--- request http error >>>"), console.log(t.statusCode),
            console.log(t.data), console.log("<<< request http error ---")), a.complete && a.complete(t);
        }
      });
  },
  saveFormId: function (t) {
    this.request({
      url: api.user.save_form_id,
      data: {
        form_id: t
      }
    });
  },
  loginBindParent: function (t) {
    if ("" == wx.getStorageSync("access_token")) return !0;
    getApp().bindParent(t);
  },
  bindParent: function (t) {
    if ("undefined" != t.parent_id && 0 != t.parent_id) {
      console.log("Try To Bind Parent With User Id:" + t.parent_id);
      var e = wx.getStorageSync("user_info");
      if (0 < wx.getStorageSync("share_setting").level) 0 != t.parent_id && getApp().request({
        url: api.share.bind_parent,
        data: {
          parent_id: t.parent_id
        },
        success: function (t) {
          0 == t.code && (e.parent = t.data, wx.setStorageSync("user_info", e));
        }
      });
    }
  },
  shareSendCoupon: function (a) {
    wx.showLoading({
      mask: !0
    }), a.hideGetCoupon || (a.hideGetCoupon = function (t) {
      var e = t.currentTarget.dataset.url || !1;
      a.setData({
        get_coupon_list: null
      }), e && wx.navigateTo({
        url: e
      });
    }), this.request({
      url: api.coupon.share_send,
      success: function (t) {
        0 == t.code && a.setData({
          get_coupon_list: t.data.list
        });
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  getauth: function (e) {
    wx.showModal({
      title: "是否打开设置页面重新授权",
      content: e.content,
      confirmText: "去设置",
      success: function (t) {
        t.confirm ? wx.openSetting({
          success: function (t) {
            e.success && e.success(t);
          },
          fail: function (t) {
            e.fail && e.fail(t);
          },
          complete: function (t) {
            e.complete && e.complete(t);
          }
        }) : e.cancel && getApp().getauth(e);
      }
    });
  },
  api: require("api.js"),
  setApi: function () {
    var o = this.siteInfo.siteroot;
    o = o.replace("app/index.php", ""), o += "addons/sd_liferuning/tp/api.php/",
      this.api = function t(e) {
        for (var a in e) "string" == typeof e[a] ? e[a] = e[a].replace("{$_api_root}", o) : e[a] = t(e[a]);
        return e;
      }(this.api);
    var t = this.api.default.index, e = t.substr(0, t.indexOf("/index.php"));
    this.webRoot = e;
  },
  webRoot: null,
  siteInfo: require("siteinfo.js"),
  currentPage: null,
  pageOnLoad: function (t) {
    this.setPageNavbar(t);

    // var that=this;
    // that.request({
    //   url: api.default.navlist,
    //   data: {
    //     bid: wx.getStorageSync('bid')
    //   },
    //   success: function (e) {
    //     wx.setStorageSync('nav', e.data);

    //       wx.setNavigationBarColor({
    //         frontColor: wx.getStorageSync('nav').frontColor,
    //         backgroundColor: wx.getStorageSync('nav').backgroundColor,

    //       })
    //   }
    // });

  },

  pageShowToast: function (t) {

    console.log("--- pageToast ---");
    var e = this.currentPage, a = t.duration || 2500, o = t.title || "", n = (t.success,
      t.fail, t.complete || null);
    e._toast_timer && clearTimeout(e._toast_timer), e.setData({
      _toast: {
        title: o
      }
    }), e._toast_timer = setTimeout(function () {
      var t = e.data._toast;
      t.hide = !0, e.setData({
        _toast: t
      }), "function" == typeof n && n();
    }, a);
  },
  //
  setPageNavbar: function (n) {
    var that = this;
    console.log("----setPageNavbar----")
    var t = wx.getStorageSync("nav");
    console.log(n, 'nav207')
    function e(t) {
      var e = !1, a = n.route || n.__route__ || null;
      console.log(n.route, 'route-----------------------')
      for (var o in t.navbar) t.navbar[o].url === "/" + a ? e = t.navbar[o].active = !0 : t.navbar[o].active = !1;
      //console.log(t.navbar,'nav存入')
      e && n.setData({
        nav: t.navbar,
      });
    }
    t && e(t)
    that.request({
      url: api.default.navlist,
      data: {
        bid: wx.getStorageSync('bid')
      },
      success: function (t) {
        1 == t.code && (e(t.data), wx.setStorageSync("nav", t.data));
      }
    });
  },

  navigatorClick: function (t, e) {
    var a = t.currentTarget.dataset.open_type;
    if (a == 'navigate') {
      wx.navigateTo({
        url: t.currentTarget.dataset.url,
      })
    }
    if ("redirect" == a) return !0;
    if ("wxapp" == a) {
      var o = t.currentTarget.dataset.path;
      "/" != o.substr(0, 1) && (o = "/" + o)
      // wx.navigateToMiniProgram({
      //   appId: t.currentTarget.dataset.appid,
      //   path: o,
      //   complete: function (t) {
      //     console.log(t);
      //   }
      // });
    }
    if ("tel" == a) {
      var n = t.currentTarget.dataset.tel;
      wx.makePhoneCall({
        phoneNumber: n
      });
    }
    return !1;
  }
});